package com.example.eventbustest

import java.util.concurrent.*

class ChildThreadPost : Poster {
    companion object {
        val instance: ChildThreadPost by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ChildThreadPost()
        }
    }

    private val executor: Executor = ThreadPoolExecutor(
        0, 4,
        0L, TimeUnit.MILLISECONDS,
        LinkedBlockingQueue<Runnable>()
    )

    override fun post(item: MySubscription, event: Any) {
        executor.execute {
            invokeMethod(item, event)
        }
    }

    private fun invokeMethod(item: MySubscription, event: Any) {
        item.method.method.invoke(item.subscribe, event)
    }
}